#include "pm/taskhistory.h"

using namespace pm;


TaskHistory::TaskHistory()
{
}

TaskHistory::TaskHistory(wxSQLite3ResultSet& res)
: m_task(res)
, m_updateDate(res.GetDate(wxT("update_date")))
{
}


GetTaskHistory::GetTaskHistory(db::ID idProject)
: m_idProject(idProject)
{
}


TaskVsInitial::TaskVsInitial(wxSQLite3ResultSet& res)
: m_idTask (res.GetInt   (wxT("id")))
, m_name   (res.GetString(wxT("name")))
, m_todo   (res.GetDouble(wxT("todo")))
, m_done   (res.GetDouble(wxT("done")))
, m_initial(res.GetDouble(wxT("initial")))
{
}


const char* GetTaskHistoryByProject::QUERY = 
"select id, name, unit_cost, quantity, end_date, update_date, id_project, id_tag "
"from view_tasks_history "
"where id_project = ? and update_date is not null "
"order by update_date asc; "
;

GetTaskHistoryByProject::GetTaskHistoryByProject(db::ID idProject)
: GetTaskHistory(idProject)
{
}

wxSQLite3Statement GetTaskHistoryByProject::prepare(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement(QUERY);
	stmt.Bind(1, m_idProject);
	return stmt;
}


const char* GetTaskHistoryByTask::QUERY = 
"select id, name, unit_cost, quantity, end_date, update_date, id_project, id_tag "
"from view_tasks_history "
"where id_project = ? and id = ? and update_date is not null "
"order by update_date asc; "
;

GetTaskHistoryByTask::GetTaskHistoryByTask(db::ID idProject, db::ID idTask)
: GetTaskHistory(idProject)
, m_idTask(idTask)
{
}

wxSQLite3Statement GetTaskHistoryByTask::prepare(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement(QUERY);
	stmt.Bind(1, m_idProject);
	stmt.Bind(2, m_idTask);
	return stmt;
}


const char* GetTaskHistoryByDate::QUERY = 
"select id, name, unit_cost, quantity, end_date, cost_done, max(th.update_date) as update_date, id_project, id_tag "
"from view_tasks_history th "
"	left join ( "
"		select id_task, sum(unit_cost * quantity) as cost_done "
"		from costs "
"		where date <= ? "
"		group by id_task "
"	) c "
"	on c.id_task = th.id "
"where th.id_project = ? and th.update_date <= ? "
"group by id;"
;

GetTaskHistoryByDate::GetTaskHistoryByDate(db::ID idProject, const wxDateTime& date)
: m_idProject(idProject)
, m_date(date)
{
}

wxSQLite3Statement GetTaskHistoryByDate::prepare(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement(QUERY);
	stmt.BindDate(1, m_date);
	stmt.Bind(2, m_idProject);
	stmt.BindDate(3, m_date);
	return stmt;
}


GetTasksVsInitial::GetTasksVsInitial(db::ID idProject)
: m_idProject(idProject)
{
}

wxSQLite3Statement GetTasksVsInitial::prepare(wxSQLite3Database& db)
{
	return db.PrepareStatement("select * from view_tasks_vs_initial");
}


CreateInitialTaskHistory::CreateInitialTaskHistory(const Task& task)
: m_task(task)
{
}

bool CreateInitialTaskHistory::query(wxSQLite3Database& db)
{
	wxSQLite3Statement stmt = db.PrepareStatement("insert into tasks_history values(?, ?, ?, ?, ?, null)");
	stmt.Bind(1, m_task.id());
	stmt.Bind(2, m_task.unit_cost());
	stmt.Bind(3, m_task.quantity());
	stmt.BindDate(4, m_task.end_date());
	if (m_task.id_tag() == db::Object::undefined) {
		stmt.BindNull(5);
	} else {
		stmt.Bind(5, m_task.id_tag());
	}
	return stmt.ExecuteUpdate() == 1;
}